package com.open.rbac.config;

import jakarta.validation.Validation;
import jakarta.validation.Validator;
import jakarta.validation.ValidatorFactory;
import org.hibernate.validator.HibernateValidator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Hibernate校验框架配置
 *
 * @author Riche's
 * @since 2025/5/24
 */
@Configuration
public class ValidatorConfig {

    /**
     * hibernate-validator
     * 1、普通模式（默认）
     *      会校验完所有的属性，然后返回所有的验证失败信息
     * 2、快速失败返回模式
     * 　　只要有一个验证失败，则返回
     */
    @Bean
    public Validator validator() {
        try (ValidatorFactory validatorFactory = Validation.byProvider(HibernateValidator.class)
                .configure()
                .addProperty("hibernate.validator.fail_fast", "true")
                .buildValidatorFactory()) {
            return validatorFactory.getValidator();
        }
    }

}
